package com.hluobo.gnnulibrary.view;

import com.hluobo.gnnulibrary.adapter.ImageSliderAdapter;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;

public class ImageSliderView  extends HorizontalScrollView{
	
	private static final String TAG = "com.hluobo.gnnulibrary";

	private ImageSliderAdapter mAdapter;
	private ImageSliderItemClickListener mListener;
	private LinearLayout wraper;
	
	public ImageSliderView(Context context) {
		this(context, null);
	}
	
	public ImageSliderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initWraper(context);
    }
	
	private void initWraper(Context context){
		wraper = new LinearLayout(context);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
		wraper.setLayoutParams(params);
		wraper.setOrientation(LinearLayout.HORIZONTAL);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// Auto-generated method stub
		
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		Log.e(TAG, "heightMode====>"+getModeString(heightMode));
		Log.e(TAG, " widthMode====>"+getModeString(widthMode));
		
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
//		Log.i(TAG, "widthMeasureSpec = "+widthMeasureSpec+", heightMeasureSpec"+heightMeasureSpec);
		
//		int width = measure(widthMeasureSpec);
//		int height = measure(heightMeasureSpec);
//		setMeasuredDimension(width, height);
		
//		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
//		Log.w(TAG, "view width="+widthSize+"; mode="+getModeString(widthMode));
//		
//		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//		int heightSize = MeasureSpec.getSize(widthMeasureSpec);
//		Log.w(TAG, "view height="+heightSize+"; mode="+getModeString(MeasureSpec.getMode(heightMeasureSpec)));
//		
//		if(heightMode == MeasureSpec.UNSPECIFIED){
//			heightSize += mAdapter.getView(0, null, null).getMeasuredHeight();
//		}
//		
//		//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//		Log.i(TAG, "Final height="+heightSize);
//		setMeasuredDimension(720, 80);
		
//		Log.i(TAG, "get id -->"+getId());
//		
//		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
//        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//		
//		int mItemCount;
//		int childWidth = 0;
//        int childHeight = 0;
//		
//    	mItemCount = mAdapter == null ? 0 : mAdapter.getCount();
//        if (mItemCount > 0 && (widthMode == MeasureSpec.UNSPECIFIED ||
//                heightMode == MeasureSpec.UNSPECIFIED)) {
//        	final View child = obtainView();
//        	measureItem(child, widthMeasureSpec);
//        	childWidth = child.getMeasuredWidth();
//            childHeight = child.getMeasuredHeight();
//            Log.e(TAG, "childWidth = "+childWidth+"; childHeight = "+childHeight);
//        }
//        
//        if (widthMode == MeasureSpec.UNSPECIFIED) {
//            widthSize = getPaddingLeft() + getPaddingRight() + childWidth +
//                    getVerticalScrollbarWidth();
//        }
//        
//        Log.e(TAG, "widthMode====>"+widthMode);
//        Log.e(TAG, "heightMode====>"+heightMode);
//
//        if (heightMode == MeasureSpec.UNSPECIFIED) {
//            heightSize = getPaddingTop() + getPaddingBottom() + childHeight +
//                    getVerticalFadingEdgeLength() * 2;
//        }
//        
//        setMeasuredDimension(widthSize , heightSize);

//        if (heightMode == MeasureSpec.AT_MOST) {
//            // TODO: after first layout we should maybe start at the first visible position, not 0
//            heightSize = measureHeightOfChildren(widthMeasureSpec, 0, NO_POSITION, heightSize, -1);
//        }
		
//		int width = measureDim(widthMeasureSpec);
//		int height = measureDim(heightMeasureSpec);
//		//adjustThingsToFit(width, height);
//		
//		Log.w(TAG, "heightMeasureSpec = "+measureHeight(heightMeasureSpec));
//		Log.w(TAG, "widthMeasureSpec = "+measureWidth(widthMeasureSpec));
//		setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));

//        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//        if (widthMode == MeasureSpec.UNSPECIFIED) {
//            return;
//        }
//        int mPaddingTop = getPaddingTop();
//        int mPaddingBottom = getPaddingBottom();
//        int mPaddingLeft = getPaddingLeft();
//        int mPaddingRight = getPaddingRight();
//
//        if (getChildCount() > 0) {
//            final View child = getChildAt(0);
//            int width = getMeasuredWidth();
//            Log.i(TAG, "width="+width+";child="+child.getMeasuredWidth());
//            if (child.getMeasuredWidth() < width) {
//            	Log.i(TAG, "哈哈哈");
//                final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
//
//                int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, mPaddingTop
//                        + mPaddingBottom, lp.height);
//                width -= mPaddingLeft;
//                width -= mPaddingRight;
//                int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
//
//                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
//            }
//        }
		
	}
	
    
    private int measureDim(int measureSpec) {
    	int result = 0;
    	int specMode = MeasureSpec.getMode(measureSpec);
    	int specSize = MeasureSpec.getSize(measureSpec);
    	if (specMode == MeasureSpec.EXACTLY) {
    	result = specSize;
    	} else {
    	//result = someCalculationOfSpaceNeeded();
    	if (specMode == MeasureSpec.AT_MOST) {
    	result = Math.min(result, specSize);
    	}
    	}
    	return(result);
    	}
    
    View obtainView(){
    	View child = mAdapter.getView(0, null, wraper);
    	ViewGroup.LayoutParams lp = child.getLayoutParams();
    	Log.i(TAG, "image width="+lp.width);
    	Log.i(TAG, "image height="+lp.height);
    	return child;
    }

	
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        Log.i(TAG, "measureWidth model --> "+getModeString(specMode));
        
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
        	View child = mAdapter.getView(0, null, wraper);
        	ViewGroup.LayoutParams lp = child.getLayoutParams();
            result = lp.width;
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    }
    private int measureHeight(int measureSpec) {
    	int result = 0;
    	int specMode = MeasureSpec.getMode(measureSpec);
    	int specSize = MeasureSpec.getSize(measureSpec);
    	
    	Log.i(TAG, "measureHeight model --> "+getModeString(specMode));
    	
    	if (specMode == MeasureSpec.EXACTLY) {
    		// We were told how big to be
    		result = specSize;
    	} else {
    		// Measure the text
    		View child = mAdapter.getView(0, null, wraper);
    		ViewGroup.LayoutParams lp = child.getLayoutParams();
    		result = lp.height;
    		if (specMode == MeasureSpec.AT_MOST) {
    			// Respect AT_MOST value if that was what is called for by measureSpec
    			result = Math.min(result, specSize);
    		}
    	}
    	
    	return result;
    }
    
    private void measureItem(View child, int widthMeasureSpec) {
        ViewGroup.LayoutParams p = child.getLayoutParams();
        if (p == null) {
            p = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }

        int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec,
                this.getPaddingLeft() + this.getPaddingRight(), p.width);
        int lpHeight = p.height;
        int childHeightSpec;
        if (lpHeight > 0) {
            childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
        } else {
            childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(childWidthSpec, childHeightSpec);
    }
    
	private String getModeString(int mode){
		String modeStr = null;
		
		switch (mode) {
		case MeasureSpec.AT_MOST:
			modeStr = "AT_MOST";
			break;
		case MeasureSpec.EXACTLY:
			modeStr = "EXACTLY";
			break;
		case MeasureSpec.UNSPECIFIED:
			modeStr = "UNSPECIFIED";
			break;
		}
		
		return modeStr;
	}
	
	public void setAdapter(ImageSliderAdapter adapter){
		mAdapter = adapter;
		if(null != mAdapter){
			for(int i=0; i<mAdapter.getCount(); i++){
				View view = mAdapter.getView(i, null, wraper);
				//Log.i(TAG, "Image width = "+view.getWidth());
				wraper.addView(view, i);
				
				// 设置监听事件
				if(null != mListener)
					mListener.onItemClick(view, i);
			}
			this.addView(wraper);
		}
	}
	
	public void setOnItemClickListener(ImageSliderItemClickListener listener){
		mListener = listener;
	}
	
	public interface ImageSliderItemClickListener{
		void onItemClick(View view, int position);
	}

}
